<script setup>

import {Lock, User} from "@element-plus/icons-vue"
import {ref} from "vue";
import {ElMessage} from "element-plus";
import {useRouter} from "vue-router";
import {insertUser, userInfo, userLogin} from "@/api/user.js";
import {useUserInfoStore} from "@/stores/userInfo.js";

const formLabelWidth = '160px'
const isRegister = ref(false);    //控制登录注册界面切换
const registerData = ref({
  id: '',
  password: '',
  rePassword: '',
  name:'',
  email:'',
  role:'0',
  gender:''
});

//获取当前用户信息
const userInfoStore = useUserInfoStore();
const getUserInfo = () => {
  return userInfo().then(res => {
    userInfoStore.setUserInfo(res.data);
  });
};

//检查两次密码
const checkRePassword = (rule,value, callback) => {
  if (value === '') {
    callback(new Error('请再次确认密码'))
  } else if (value !== registerData.value.password) {
    callback(new Error('两次密码不一样'))
  } else {
    callback()
  }
}

//登录表单检查规则
const rules = {
  id: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
    {min: 3, max: 16, message: '长度为5-16位', trigger: 'blur'},
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {min: 3, max: 16, message: '长度为5-16位', trigger: 'blur'},
  ],
  rePassword: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {validator: checkRePassword, trigger: 'blur'},
  ]
}
const router = useRouter()
const form = ref(null)

//登录
const login = () => {
  form.value.validate((valid) => {
    if (valid) {
      userLogin(registerData.value).then(res => {
        if (res.status) {
          getUserInfo().then(() => { // 等待getUserInfo完成
            ElMessage({
              message: res.message ? res.message : '登录成功！',
              type: 'success',
            });
            if (userInfoStore.userInfo.role ===0) {
              router.push("/user/checkin");
            } else {
              router.push("/admin/userManagement");
            }
          });
        } else {
          ElMessage.error(res.message);
        }
      });
    }
  });
};

//用于清空登录注册信息
const clearRegisterData = () => {
  registerData.value = {
    id: '',
    password: '',
    rePassword: '',
    name:'',
    email:'',
    role:'0',
    gender:''
  }
}

// 注册
const register = () => {
  form.value.validate((valid)=>{
    if (valid) {
      insertUser(registerData.value).then(res=>{
        if (res.status) {
          ElMessage.success("注册成功")
        }
      })
    }
  })
};

</script>

<template>
  <div class="container">
    <div class="form-container">
      <!-- 注册表单 -->
      <el-form ref="form" size="large" autocomplete="on" v-if="isRegister" :model="registerData" :rules="rules">
        <el-form-item>
          <h1>酒店预约管理系统</h1>
        </el-form-item>
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>

        <el-form-item label="用户名" :label-width="formLabelWidth" prop="id">
          <el-input v-model="registerData.id" autocomplete="off" placeholder="请输入用户名" />
        </el-form-item>

        <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
          <el-input v-model="registerData.password" autocomplete="off" placeholder="请输入密码" />
        </el-form-item>

        <el-form-item label="重复密码" :label-width="formLabelWidth" prop="rePassword">
          <el-input v-model="registerData.rePassword" autocomplete="off" placeholder="请再次输入密码" />
        </el-form-item>

        <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="registerData.name" autocomplete="off" placeholder="请输入名称" />
        </el-form-item>

        <el-form-item label="邮箱" :label-width="formLabelWidth" prop="email">
          <el-input v-model="registerData.email" autocomplete="off" placeholder="请输入邮箱" />
        </el-form-item>

        <el-form-item label="性别" :label-width="formLabelWidth" prop="gender">
          <el-select v-model="registerData.gender" placeholder="请选择性别">
            <el-option label="男" value="男" />
            <el-option label="女" value="女" />
          </el-select>
        </el-form-item>

        <!-- 注册按钮 -->
        <el-form-item>
          <el-button class="button" type="primary" auto-insert-space @click="register">
            注册
          </el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = false;clearRegisterData()">
            ← 返回
          </el-link>
        </el-form-item>
      </el-form>

      <!-- 登录表单 -->
      <el-form ref="form" size="large" autocomplete="off" v-else :model="registerData" :rules="rules">
        <el-form-item>
          <h1>酒店预约管理系统</h1>
        </el-form-item>
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="id">
          <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="registerData.id"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input name="password" :prefix-icon="Lock" type="password" placeholder="请输入密码"
                    v-model="registerData.password"></el-input>
        </el-form-item>

        <!-- 登录按钮 -->
        <el-form-item>
          <el-button class="button" type="primary" auto-insert-space @click="login">
            登录
          </el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = true;clearRegisterData()">
            注册 →
          </el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.container {
  background-image: url('@/assets/1.jpg');
  background-size: cover; /* 背景图片覆盖整个容器 */
  background-position: center; /* 背景图片居中显示 */
  display: flex;
  justify-content: center; /* 水平居中 */
  align-items: center; /* 垂直居中 */
  height: 100vh; /* 容器高度设为100%视口高度 */
  overflow: hidden; /* 防止内容溢出容器 */


.form-container {
  background-color: white;
  padding: 40px; /* 增加内边距，使表单内容不贴边 */
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* 添加阴影，增加立体感 */
  border-radius: 12px; /* 圆角处理，使边角更柔和 */
  max-width: 400px; /* 限制表单最大宽度，使其更紧凑 */
  width: 100%; /* 确保表单在小屏幕上也能占据可用宽度 */
  text-align: center; /* 表单内容居中显示 */
}
  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;

    .title {
      margin: 0 auto;
    }

    .button {
      width: 100%;
    }

    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>